﻿using Candy.Web.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace Candy.Web.Model
{
    public class FileManager
    {


        //public IQueryable<FileDto> GetFastaFiles()
        //{

        //    string path = WebConfigurationManager.AppSettings["FastaDir"];
        //    List<FileDto> files = new List<FileDto>();
        //    if (string.IsNullOrEmpty(path)) return files.AsQueryable();
        //    DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(path));
        //    foreach (FileInfo file in di.GetFiles())
        //    {
        //        files.Add(new FileDto { FileName = file.Name });
        //    }
        //    return files.AsQueryable();
        //}

        //public void SaveFastaFile(FileDto file)
        //{
        //    string uploadDir = GetUploadProjetDir(file.ProjectId);
        //    string fullFileName = GetFastaUploadFilePath(file.ProjectId, file.FileName);
        //    if (!string.IsNullOrEmpty(uploadDir))
        //    {
        //        if (!Directory.Exists(uploadDir))
        //        {
        //            Directory.CreateDirectory(uploadDir);
        //        }
        //        if (File.Exists(fullFileName))
        //        {
        //            File.Delete(fullFileName);
        //            //string ext = fullFileName.Substring(fullFileName.LastIndexOf("."));
        //            //string fName = fullFileName.Substring(0, fullFileName.LastIndexOf("."));
        //            //fullFileName = string.Format("{0}_1{1}", fName, ext);
        //        }
        //        File.WriteAllBytes(fullFileName, file.FileContent);
        //    }
        //}

        public void SaveProfileFile(RegistrationData file)
        {
            string fullFileName = GetProfileUploadFilePath(file.UserName);

            if (File.Exists(fullFileName))
            {
                File.Delete(fullFileName);
            }
            try
            {
                File.WriteAllBytes(fullFileName, file.ProfilePicture);
            }
            catch (Exception e)
            {
                Trace.WriteLine("Failed to write profile picture for user " + file.UserName);
            }

        }

        public Uri GetProfilePictureUrl(string username)
        {
            string filePath = GetProfileUploadFilePath(username);
            string relServerPath = "";
            if (!File.Exists(filePath))
            {
                relServerPath = WebConfigurationManager.AppSettings["ProfileDir"] + "/default.jpg";
            }
            else
            {
                relServerPath = WebConfigurationManager.AppSettings["ProfileDir"] + "/" + username + ".jpg";
            }
            relServerPath = relServerPath.Replace("~/", "");
            var virtualDir = HttpContext.Current.Request.ApplicationPath;
            Uri currentHost = new Uri("http://" + HttpContext.Current.Request.Headers["Host"] + virtualDir + "/");
            string uriStr = new Uri(currentHost, relServerPath).ToString();
            return new Uri(uriStr, UriKind.Absolute);

        }

        //public void DeleteProjectDir(int projectId)
        //{
        //    string uploadDir = GetUploadProjetDir(projectId);
        //    if (!string.IsNullOrEmpty(uploadDir))
        //    {
        //        if (Directory.Exists(uploadDir))
        //        {
        //            Directory.Delete(uploadDir, true);
        //        }
        //    }
        //}

        //private string GetFastaUploadDirRoot()
        //{
        //    string uploadDir = WebConfigurationManager.AppSettings["FastaDir"];
        //    if (!string.IsNullOrEmpty(uploadDir))
        //    {
        //        if (uploadDir.IndexOf("~/") == 0)
        //            uploadDir = HttpContext.Current.Server.MapPath(uploadDir);
        //        if (uploadDir.LastIndexOf("/") == uploadDir.Length - 1)
        //            uploadDir = uploadDir.Substring(0, uploadDir.Length - 1);
        //    }
        //    return uploadDir;
        //}

        private string GetProfileUploadDirRoot()
        {
            string uploadDir = WebConfigurationManager.AppSettings["ProfileDir"];
            if (!string.IsNullOrEmpty(uploadDir))
            {
                if (uploadDir.IndexOf("~/") == 0)
                    uploadDir = HttpContext.Current.Server.MapPath(uploadDir);
                if (uploadDir.LastIndexOf("/") == uploadDir.Length - 1)
                    uploadDir = uploadDir.Substring(0, uploadDir.Length - 1);
            }
            return uploadDir;
        }

        //private string GetUploadProjetDir(int projectId)
        //{
        //    string uploadDir = GetFastaUploadDirRoot();
        //    if (!string.IsNullOrEmpty(uploadDir))
        //    {
        //        uploadDir = string.Format("{0}/{1}", uploadDir, projectId);
        //    }
        //    return uploadDir;
        //}



        //private string GetFastaUploadFilePath(int projectId, string fileName)
        //{
        //    string uploadDir = GetUploadProjetDir(projectId);
        //    if (!string.IsNullOrEmpty(uploadDir))
        //    {
        //        uploadDir = string.Format("{0}/{1}", uploadDir, fileName);
        //    }
        //    return uploadDir;
        //}


        private string GetProfileUploadFilePath(string userName)
        {
            string uploadDir = GetProfileUploadDirRoot();
            if (!string.IsNullOrEmpty(uploadDir))
            {
                uploadDir = string.Format("{0}/{1}.jpg", uploadDir, userName);
            }
            return uploadDir;
        }

        //internal void DeletFile(FileDto file)
        //{
        //    string dir = GetFastaUploadFilePath(file.ProjectId, file.FileName);
        //    if (File.Exists(dir))
        //        File.Delete(dir);
        //}
    }
}